package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.passport.pojo.param.AdminAddNewParam;
import cn.tedu.csmall.passport.pojo.param.AdminLoginInfoParam;
import cn.tedu.csmall.passport.pojo.param.AdminUpdateInfoParam;
import cn.tedu.csmall.passport.pojo.param.AdminUpdatePasswordParam;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.passport.pojo.vo.AdminStandardVO;
import cn.tedu.csmall.passport.pojo.vo.PageData;
import cn.tedu.csmall.passport.security.LoginPrincipal;
import cn.tedu.csmall.passport.service.IAdminService;
import cn.tedu.csmall.passport.web.JsonResult;
import cn.tedu.csmall.passport.web.ServiceCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.xml.ws.Service;

@RestController
@RequestMapping("/admin")
@Api(tags = "01. 管理员管理模块")
@Slf4j
@Validated
public class AdminController {

    @Autowired
    private IAdminService adminService;

    //管理员登录
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 1)
    public JsonResult login(AdminLoginInfoParam adminLoginInfoParam) {
        log.debug("开始处理【管理员登录】的请求，参数：{}", adminLoginInfoParam);
        String jwt=adminService.login(adminLoginInfoParam);
        return JsonResult.ok(jwt);
    }

    // http://localhost:9393/admin/add-new
    //添加管理员
    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 2)
    public JsonResult addNew(@Valid AdminAddNewParam albumAddNewParam) {

        log.debug("开始处理【添加管理员】的请求，参数：{}", albumAddNewParam);
        adminService.addNew(albumAddNewParam);
        return JsonResult.ok();
    }


    // http://localhost:9393/admin/list
    //查询管理员列表
    @GetMapping("/list")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询管理员列表失败,请提供正确的页码值!!!") Integer page) {
        log.debug("开始处理【查询管理员列表】的请求,页码:{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<AdminListItemVO> pageData = adminService.list(page);

        /*    log.debug("当事人的ID:{}", loginPrincipal.getId());
        log.debug("当事人的用户名:{}", loginPrincipal.getUsername());*/
        return JsonResult.ok(pageData);
    }


    // http://localhost:9393/admin/add-new
    //根据ID删除管理员
    @PostMapping("/delete")
    @ApiOperation("根据ID删除管理员")
    @ApiOperationSupport(order = 4)
    //参数注解
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除管理员失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据ID删除管理员】的请求，参数：{}", id);
        adminService.delete(id);

        return JsonResult.ok();
    }

    // http://localhost:9393/admin/standard
    @GetMapping("/standard")
    @ApiOperation("根据ID查询管理员详情")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询管理员详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询管理员详情】的请求，参数：{}", id);
        AdminStandardVO result = adminService.getStandardById(id);
        return JsonResult.ok(result);
    }




    @PostMapping("/updateAdminPassword")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperation("修改管理员密码")
    @ApiOperationSupport(order = 6)
    public JsonResult update(@Valid AdminUpdatePasswordParam adminUpdatePasswordParam,
                             @Range(min = 1, message = "根据ID修改管理员密码失败，请提交合法的ID值！")
                             @RequestParam Long id
    ) {
        log.debug("开始处理【修改管理员密码】的请求，参数：{}", adminUpdatePasswordParam);
        adminService.updatePasswordById(id, adminUpdatePasswordParam);
        log.debug("处理【修改管理员密码】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperation("修改管理员基本信息")
    @ApiOperationSupport(order = 7)
    public JsonResult update(@Valid AdminUpdateInfoParam adminUpdateInfoParam,
                             @Range(min = 1, message = "根据ID修改管理员基本信息失败，请提交合法的ID值！")
                             @RequestParam Long id
    ) {
        log.debug("开始处理【修改管理员基本信息】的请求，参数：{}", adminUpdateInfoParam);
        adminService.updateInfoById(id, adminUpdateInfoParam);
        log.debug("处理【修改管理员基本信息】的请求，完成！");
        return JsonResult.ok();
    }
}
